/*
 * author      : 吴中勤 
 * date        : 2019/3/23
 * description : TODO
 */

package org.wzq.pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest001 {
    /*
     先创建 coreSize
     然后丢到 queue里面
     queue满了后, 创建直到 maxSize
     再多就 reject(rejectHandler)
     */
    BlockingQueue<Runnable> blockingQueue1 = new ArrayBlockingQueue<Runnable>(2);
    ThreadPoolExecutor executor1 = new ThreadPoolExecutor(1, 2, 5, TimeUnit.SECONDS, blockingQueue1);

    BlockingQueue<Runnable> blockingQueue2 = new ArrayBlockingQueue<Runnable>(2);
    ThreadPoolExecutor executor2 = new ThreadPoolExecutor(1, 3, 5, TimeUnit.SECONDS, blockingQueue2, new MyThreadFactory());

    public static void main(String[] args) {
        ThreadPoolTest001 test001 = new ThreadPoolTest001();

        for (int i = 5; i > 0; i--) {
            test001.executor1.execute(() -> {
                System.out.println("queue1.size=" + test001.blockingQueue1.size());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("task1: thread id=" + Thread.currentThread().getId());
            });
        }


//        for (int i = 5; i > 0; i--) {
//            test001.executor2.execute(() -> {
//                System.out.println("queue2.size=" + test001.blockingQueue2.size());
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("task2: thread id=" + Thread.currentThread().getId());
//            });
//        }
    }
}
